package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.YueGuangGuangServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "yueguangguang")
public class YueGuangGuangAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(YueGuangGuangAdaptor.class);
	
	
	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
	
		String productid = orderBean.getProductCode();
		String mobile = orderBean.getPhoneNo();
		String sporderid = orderBean.getIds();
		String price = orderBean.getProductPrice().toString();
		String back_url = BetweenProperties.CALLBACK_URL+"YueGuangg";
		log.info("between- 月光光订单YueGuangGuangService.order()  订购请求productid:" + productid+",mobile:"+mobile+",sporderid:"+sporderid+",price"+price+",back_url:"+back_url);

		String result = YueGuangGuangServiceImpl.service.order(productid, mobile, sporderid,back_url);
		log.info("between- 月光光订单YueGuangGuangService.order() 订购响应response<>:" + result);
		
	
		
		JSONObject json = JSONObject.fromObject(result);
		String code = json.getString("resultno");
		log.info("between- 月光光订单YueGuangGuangService.order()返回的状态码：" + code);

		if ("0".equals(code) || "1".equals(code) || "2".equals(code)) {
			orderBean.setMessage("订购中");
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
		} else {
			orderBean.setMessage("订购失败");
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
	
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	
	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		String cstmOrderNo = orderBean.getIds();
		log.info("between -月光光状态YueGuangGuangService.orderStatus() 查询请求cstmOrderNo:" + cstmOrderNo);
		String result = YueGuangGuangServiceImpl.service.orderStatus(cstmOrderNo);
		log.info("between- 月光光状态YueGuangGuangService.orderStatus() 查询响应response<> :" + result);

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("resultno");

		if ("1".equals(code)) {

			orderBean.setMessage("充值成功");
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
		} else if ("0".equals(code) || "2".equals(code)) {
			orderBean.setMessage(parseResultCodeToMsg(code));
			orderBean.setErrorMsg(parseResultCodeToMsg(code));
			orderBean.setStatus("1");
		} else {
			orderBean.setMessage(parseResultCodeToMsg(code));
			orderBean.setErrorMsg(parseResultCodeToMsg(code));
			orderBean.setStatus("8");
		}

		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
	
	public String parseResultCodeToMsg(String code) {
		String msg = "";
		if ("0".equals(code)) {
			msg = "等待充值，预下单时表示下单成功";
		}else if ("1".equals(code)) {
			msg = "充值成功";
		}else if ("2".equals(code)) {
			msg = "充值中";
		}else if ("9".equals(code)) {
			msg = "充值失败已退款";
		}else if ("5001".equals(code)) {
			msg = "代理商不存在";
		}else if ("5002".equals(code)) {
			msg = "代理商余额不足";
		}else if ("5003".equals(code)) {
			msg = "此商品暂时不可购买";
		}else if ("5004".equals(code)) {
			msg = "充值号码与所选商品不符";
		}else if ("5005".equals(code)) {
			msg = "充值请求验证错误";
		}else if ("5006".equals(code)) {
			msg = "代理商订单号重复";
		}else if ("5007".equals(code)) {
			msg = "订单不存在";
		}else if ("5008".equals(code)) {
			msg = "交易亏损不能充值";
		}else if ("5009".equals(code)) {
			msg = "Ip不符";
		}else if ("5010".equals(code)) {
			msg = "商品编号与充值金额不符";
		}else if ("5011".equals(code)) {
			msg = "商品数量不支持";
		}else if ("5012".equals(code)) {
			msg = "缺少必要参数或参数值不合法";
		}else if ("9999".equals(code)) {
			msg = "未知错误,需进入平台查询核实";
		}
		
		return msg;
	}
}
